package BLL;
import org.jfree.chart.ChartFactory;
import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.labels.StandardPieSectionLabelGenerator;
import org.jfree.chart.plot.PiePlot3D;
import org.jfree.data.general.DefaultPieDataset;

import java.awt.*;
import java.text.DecimalFormat;
import java.text.NumberFormat;

/**
 * @author 陈显彩
 * @version 2021.0601
 * @since 1.8
 */

/**
 * 图表
 */
public class Chart {
    /**
     * 生成数据集实例
     * @param used 已用空间容量
     * @param left 剩余空间容量
     * @return  图数据集
     */
    public static DefaultPieDataset PieData(double used, double left){
        DefaultPieDataset dataset = new DefaultPieDataset();
        dataset.setValue("已用空间", used);
        dataset.setValue("剩余空间", left);
        return dataset;
    }
    
    /**
     *
     * @param used 已用空间容量
     * @param left 剩余空间容量
     * @param chartName 图表名称
     * @return 图表面板
     */
    public static ChartPanel GetPieChart(double used, double left, String chartName){
        ChartPanel MyPanel;
        DefaultPieDataset dataset = PieData(used, left);
        JFreeChart MyChart = ChartFactory.createPieChart3D(chartName, dataset, true, false, false);
    
        PiePlot3D MyPlot = (PiePlot3D) MyChart.getPlot();
        DecimalFormat decimal = new DecimalFormat("0.00%");
        NumberFormat number = NumberFormat.getNumberInstance();
        StandardPieSectionLabelGenerator label = new StandardPieSectionLabelGenerator("{0}={1}({2})", number, decimal);
        
        MyPlot.setNoDataMessage("没有数据");
        MyPlot.setCircular(false);
        MyPlot.setLabelGap(0.02d);
        MyPlot.setIgnoreNullValues(true);
        MyPlot.setIgnoreZeroValues(true);
        
        MyPanel = new ChartPanel(MyChart, true);
        MyChart.getTitle().setFont(new Font("宋体", Font.BOLD, 20));
        PiePlot3D plot = (PiePlot3D) MyChart.getPlot();
        plot.setLabelFont(new Font("宋体", Font.BOLD, 10));
        plot.setSectionPaint("已用空间", Color.decode("#EBB205"));
        plot.setSectionPaint("剩余空间", Color.decode("#426CB6"));
        MyChart.getLegend().setItemFont(new Font("黑体", Font.BOLD, 10));
        return MyPanel;
    }
}


